package com.springcloud.auth.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springcloud.auth.entity.TUserInfo;
import com.springcloud.auth.mapper.TUserInfoMapper;
import com.springcloud.auth.service.TUserInfoService;
import com.springcloud.auth.utils.GenerateUtil;
import com.springcloud.commom.api.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hzl
 * @since 2020-09-30
 */
@Service
@Transactional
public class TUserInfoServiceImpl extends ServiceImpl<TUserInfoMapper, TUserInfo> implements TUserInfoService {

    @Resource
    private TUserInfoMapper mapper;

    @Override
    public R register(TUserInfo info) {
        QueryWrapper<TUserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TUserInfo::getUserName,info.getUserName());

        TUserInfo selInfo = mapper.selectOne(queryWrapper);
        if (selInfo != null) {
            return R.fail("该用户已经注册过了!");
        }
        //用户名做盐
        String salt = GenerateUtil.getSalt(info.getUserName());
        info.setSalt(salt);
        String pwd = GenerateUtil.genePwd(info.getPassword(), salt);
        info.setPassword(pwd);
        info.setGender(1);

        mapper.insert(info);

        return R.success();
    }
}
